// Golimiy kakoy to klass poluchilsa , nujno peredelat mnogo func's 
// all options , when ship docked

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using SpaceWolf.Graphics;

namespace SpaceWolf.GameLogic
{
    class Docked
    {
        const int NO_OF_RANKS=9;
        struct rank
        {
            public int score;
            public string title;

            public rank(int score, string title)
            {
                this.score = score;
                this.title = title;
            }
        };

        SpaceWolf.Graphics.Texture SmallPlanetTexture;
        SpaceWolf.Graphics.Texture MessageBackGround;
        Trade tradeMarket;
        Commander cmdr;
        rank[] rating;

        public Docked(Trade tradeMarket, Commander cmdr)
        {
            rating = new rank[NO_OF_RANKS];
            rating[0] = new rank(0x0000, "Harmless");
            rating[1] = new rank(0x0008, "Mostly Harmless");
            rating[2] = new rank(0x0010, "Poor");
            rating[3] = new rank(0x0020, "Average");
            rating[4] = new rank(0x0040, "Above Average");
            rating[5] = new rank(0x0080, "Competent");
            rating[6] = new rank(0x0200, "Dangerous");
            rating[7] = new rank(0x0A00, "Deadly");
            rating[8] = new rank(0x1900, "---- E L I T E ---");
            
            this.tradeMarket = tradeMarket;
            this.cmdr = cmdr;
        }

        public void LoadModels()
        {
            SmallPlanetTexture = new SpaceWolf.Graphics.Texture("LittlePlanetForMap");
            MessageBackGround = new SpaceWolf.Graphics.Texture("MessageBkGrnd");
        }

        /// <summary>
        /// Displays nearest planets in my galaxy
        /// </summary>
        /// <param name="curr_galaxy"></param>
        public void display_galaxy_chart(Galaxy curr_galaxy)
        {
            galaxy_seed glx = curr_galaxy.arPlanets[cmdr.planet].PlanetGalaxy;
            galaxy_seed curr_glx=curr_galaxy.CurrentGalaxySeed;
            int[] row_used = new int[64];

            Helper.Fonts.WriteText(SpaceWolf.BaseGame.Height / 2, 10, "SHORT RANGE CHART", Color.Gold);
            
            for (int i = 0; i < 64; i++)
                row_used[i] = 0;

            for (int iCounter = 0; iCounter < curr_galaxy.arPlanets.Count; iCounter++)
            {
                
                int dx = Math.Abs(curr_galaxy.arPlanets.Count - cmdr.planet)+iCounter;
                
                if(dx>=curr_galaxy.arPlanets.Count)
                    dx = dx - curr_galaxy.arPlanets.Count;
                
                int px = (dx);
                px = (curr_glx.d - curr_galaxy.arPlanets[dx].PlanetGalaxy.d);  /* Convert to screen co-ords */
                px =(px*2)+200;

                int py = (curr_glx.b - curr_galaxy.arPlanets[dx].PlanetGalaxy.b);

                //int py = Math.Abs(glx.b+ curr_galaxy.arPlanets[dx].PlanetGalaxy.b);
                py = (py) + 300;	/* Convert to screen co-ords */

                int row = py / (16);

                if (row_used[row] == 1)
                    row++;

                if (row_used[row] == 1)
                    row -= 2;

                if (row <= 3)
                {
                    curr_glx = Galaxy.waggle_galaxy(curr_glx);
                }
                if (row_used[row] == 0)
                {

                    //row_used[row] = 1;
                    Color color = (dx == cmdr.planet) ? Color.White : Color.Gold;
                    //px -= 100;
                    //py = (row * 8-5);
                    //px = (iCounter * 40);
                    py -= 200;
                    SpriteHelper.AddSpriteToRender(SmallPlanetTexture, new Rectangle(px+10, py-15, 20, 20), new Rectangle(0, 0, 20, 20));
                    Helper.Fonts.WriteText(px , py , curr_galaxy.arPlanets[dx].Name, color);
                    if(InputManager.MouseInBox(new Rectangle(px+10,py-15,20,20)))
                    {
                        //BackGround
                        SpriteHelper.AddSpriteToRender(MessageBackGround, new Rectangle(px, py+15, MessageBackGround.Width, MessageBackGround.Height), new Rectangle(0, 0, MessageBackGround.Width, MessageBackGround.Height));
                        
                        //Texts
                        string sPopulation = string.Format("Population: {0} million",curr_galaxy.arPlanets[dx].TechData.population);
                        string sTechLevel = string.Format("Tech level: {0}", curr_galaxy.arPlanets[dx].TechData.techlevel);
                        string sEconomy = string.Format("Economy: {0}", curr_galaxy.arPlanets[dx].TechData.economy.ToString());

                        Helper.Fonts.WriteText(px +10, py + 30, curr_galaxy.arPlanets[dx].Inhabitants, Color.CornflowerBlue);
                        Helper.Fonts.WriteText(px + 10, py+43, "Dist: " + show_distance_to_planet(curr_galaxy.arPlanets[dx].PlanetGalaxy, glx), Color.Red);
                        Helper.Fonts.WriteText(px + 10, py + 56, sPopulation, Color.Red);
                        Helper.Fonts.WriteText(px + 10, py + 69, sEconomy, Color.Red);
                        if (InputManager.MouseLeftButtonJustPressed)
                        {
                            curr_galaxy.JumpToPlanet = dx;
                            if (curr_galaxy.JumpToPlanet != cmdr.planet && curr_galaxy.JumpToPlanet == dx)
                            {
                                Sounds.Sound.Play("Destination_Selected");
                         
                            }
                            
                        }
                        if (curr_galaxy.JumpToPlanet != cmdr.planet && curr_galaxy.JumpToPlanet == dx)
                        {
                            string str = string.Format("Destination to {0} selected", curr_galaxy.arPlanets[dx].Name);
                            Helper.Fonts.WriteText(BaseGame.Width / 2 - 100, BaseGame.Height - 100, str, Color.Red);
                        }
                    }
                    
                    
                }

            }
        }

        private int show_distance_to_planet(galaxy_seed hyperspace_planet, galaxy_seed glx)
        {
            //string planet_name;
            //string str;

            
            //px = ((cross_x - GFX_X_CENTRE) / (4 * GFX_SCALE)) + docked_planet.d;
            //py = ((cross_y - GFX_Y_CENTRE) / (2 * GFX_SCALE)) + docked_planet.b;

            //galaxy_seed hyperspace_planet = find_planet(px, py,glx);

            //name_planet(planet_name, hyperspace_planet);

            //gfx_clear_text_area();
            //sprintf(str, "%-18s", planet_name);
            //gfx_display_text(16, 340, str);

            return calc_distance_to_planet(glx, hyperspace_planet);

            //if (current_screen == SCR_GALACTIC_CHART)
            //{
            //    cross_x = hyperspace_planet.d * GFX_SCALE;
            //    cross_y = hyperspace_planet.b / (2 / GFX_SCALE) + (18 * GFX_SCALE) + 1;
            //}
            //else
            //{
            //    cross_x = ((hyperspace_planet.d - docked_planet.d) * (4 * GFX_SCALE)) + GFX_X_CENTRE;
            //    cross_y = ((hyperspace_planet.b - docked_planet.b) * (2 * GFX_SCALE)) + GFX_Y_CENTRE;
            //}
        }

        int calc_distance_to_planet(galaxy_seed from_planet, galaxy_seed to_planet)
        {
            int dx, dy;
            int light_years;

            dx = Math.Abs(to_planet.d - from_planet.d);
            dy = Math.Abs(to_planet.b - from_planet.b);

            dx = dx * dx;
            dy = dy / 2;
            dy = dy * dy;

            light_years = Convert.ToInt32(Math.Sqrt(dx + dy));
            light_years *= 4;

            return light_years;
        }

        private galaxy_seed find_planet(int cx, int cy,galaxy_seed glx)
        {
            int min_dist = 10000;
            int distance;
            int dx, dy;
            int i;
            galaxy_seed planet = new galaxy_seed() ;

            for (i = 0; i < 256; i++)
            {

                dx = Math.Abs(cx - glx.d);
                dy = Math.Abs(cy - glx.b);

                if (dx > dy)
                    distance = (dx + dx + dy) / 2;
                else
                    distance = (dx + dy + dy) / 2;

                if (distance < min_dist)
                {
                    min_dist = distance;
                    planet = glx;
                }

                glx=Galaxy.waggle_galaxy(glx);
                glx = Galaxy.waggle_galaxy(glx);
                glx = Galaxy.waggle_galaxy(glx);
                glx = Galaxy.waggle_galaxy(glx);
            }

            return planet;
        }

        public void GenerateMarket()
        {
            tradeMarket.generate_stock_market();
        }//Generate Market

        public void display_market_prices()
        {

            int i;

            string str = string.Format("{0} market places", tradeMarket.CurrentPlanet.Name);
            Helper.Fonts.WriteText(10, 10, str, Color.Gold);

            Helper.Fonts.WriteText(16, 40, "PRODUCT", Color.Green);
            Helper.Fonts.WriteText(220, 40, "UNIT", Color.Green);
            Helper.Fonts.WriteText(320, 40, "PRICE", Color.Green);
            Helper.Fonts.WriteText(450, 40, "FOR SALE", Color.Green);
            Helper.Fonts.WriteText(620, 40, "IN HOLD", Color.Green);

            for (i = 0; i < 17; i++)
            {
                display_stock_price(i);
            }

            //if (docked)
            //{
            //    hilite_item = -1;
            //    highlight_stock(0);
            //}
            str = string.Format("Current credits: {0:0.0}", cmdr.credits);
            Helper.Fonts.WriteText(0, 500, str);
        }//display_market_prices()

        public void display_MilitaryMarket_prices()
        {
            string str = string.Format("{0} armory", tradeMarket.CurrentPlanet.Name);
            Helper.Fonts.WriteText(10, 10, str, Color.Gold);

            Helper.Fonts.WriteText(16, 40, "Eqip ship", Color.Green);

            for (int i = 0; i < tradeMarket.Equipment.Count;i++)
            {
                Armory item = (Armory)tradeMarket.Equipment[i];
                Helper.Fonts.WriteText(0, 500, "Current credits: " + cmdr.credits);

                int y = 0;
                Color colour = Color.Wheat;
                y = i * 25 + 65;
                if (cmdr.ship.Docked)
                {
                    if (InputManager.MouseInBox(new Rectangle(10, y, 100, 17)))
                    {
                        if (cmdr.ship.CurrentLaser.Name != item.Name)
                        {
                            colour = Color.Green;
                            BaseGame.DrawLine(new Point(14, y), new Point(14, y + 15), Color.Red);
                            BaseGame.DrawLine(new Point(14, y), new Point(150, y), Color.Red);
                            BaseGame.DrawLine(new Point(150, y), new Point(150, y + 15), Color.Red);
                            BaseGame.DrawLine(new Point(14, y + 15), new Point(150, y + 15), Color.Red);

                            if (InputManager.MouseLeftButtonJustPressed)
                            {
                                if (cmdr.credits > item.Price)
                                {
                                    cmdr.credits -= item.Price;

                                    if(item is Laser)
                                        cmdr.ship.CurrentLaser = (Laser)item;
                                    if (item is CoolantUnit)
                                        cmdr.ship.Coolant= (CoolantUnit)item;
                                }
                                else
                                {
                                    Helper.Fonts.WriteText(300, y, "Not enought money");
                                }
                            }
                        }
                        //if (InputManager.MouseRightButtonJustPressed)
                        //{
                        //    //if (cmdr.current_cargo[i] > 0)
                        //    //{
                        //    //    cmdr.credits += tradeMarket.stock_market[i].current_price;
                        //    //    cmdr.current_cargo[i]--;
                        //    //}
                        //    //Helper.Fonts.WriteText(0, y, y.ToString());
                        //}
                    }
                }
                Helper.Fonts.WriteText(16, y, item.Name, colour);

                Helper.Fonts.WriteText(440, y, item.Price.ToString(),colour);
            }
        }//display_market_prices()

        private void display_stock_price(int i)
        {
            int y=0;
            string str;

            stock_item item=(stock_item)tradeMarket.stock_market[i];

            y =i * 25+65;
            
            Color colour=Color.White;

            if (cmdr.ship.Docked)
            {
                if (InputManager.MouseInBox(new Rectangle(10, y, 100, 17)))
                {
                    colour = Color.Green;
                    BaseGame.DrawLine(new Point(14, y), new Point(14, y + 15), Color.Red);
                    BaseGame.DrawLine(new Point(14, y), new Point(150, y), Color.Red);
                    BaseGame.DrawLine(new Point(150, y), new Point(150, y + 15), Color.Red);
                    BaseGame.DrawLine(new Point(14, y + 15), new Point(150, y + 15), Color.Red);

                    if (InputManager.MouseLeftButtonJustPressed)
                    {
                        int iAllCargo = cmdr.total_cargo();
                       
                        if (iAllCargo < cmdr.ship.CargoBay && cmdr.credits >= item.current_price &&
                            item.current_quantity > 0)
                        {
                            item.current_quantity -= 1;
                            cmdr.credits -= item.current_price;
                            cmdr.current_cargo[i]++;
                        }
                        //Helper.Fonts.WriteText(0, y, y.ToString());
                    }

                    if (InputManager.MouseRightButtonJustPressed)
                    {
                        if (cmdr.current_cargo[i] > 0)
                        {
                            cmdr.credits += item.current_price;
                            cmdr.current_cargo[i]--;
                        }
                        //Helper.Fonts.WriteText(0, y, y.ToString());
                    }
                }
            }
            Helper.Fonts.WriteText(16, y, item.name, colour);

            Helper.Fonts.WriteText(240, y, item.units.ToString());
            str = string.Format("{0}", item.current_price);
            Helper.Fonts.WriteText(330, y, str);

            if (item.current_quantity > 0)
                str = string.Format("{0:d}{1:s}", item.current_quantity,
                                    item.units.ToString());
            else
                str="-";

            Helper.Fonts.WriteText(460, y, str);

            if (cmdr.current_cargo[i] > 0)
                str = string.Format("{0:d}{1:s}",cmdr.current_cargo[i],
                                      item.units.ToString());
            else
                str="-";

            Helper.Fonts.WriteText(670, y, str);
        }//display_stock_price(int i)

        public void display_commander_status()
        {
            string str=string.Empty;
            int i;
            //int x, y;
            //int condition;

            str=string.Format("COMMANDER {0}", cmdr.name);

            Helper.Fonts.WriteText(SpaceWolf.BaseGame.Height / 2,10, str, Color.Gold);

            Helper.Fonts.WriteText(16, 58, "Present System:", Color.Green);

            //if (!witchspace)
            //{
                //name_planet(planet_name, docked_planet);
                //capitalise_name(planet_name);
            Helper.Fonts.WriteText(350, 58, tradeMarket.CurrentPlanet.Name);
            //}

            Helper.Fonts.WriteText(16, 94, "Hyperspace System:", Color.Green);
           
            Helper.Fonts.WriteText(350, 94, tradeMarket.CurrentPlanet.Name);

            //if (docked)
            //else
            //{
            //    condition = 1;

            //    for (i = 0; i < MAX_UNIV_OBJECTS; i++)
            //    {
            //        type = universe[i].type;

            //        if ((type == SHIP_MISSILE) ||
            //            ((type > SHIP_ROCK) && (type < SHIP_DODEC)))
            //        {
            //            condition = 2;
            //            break;
            //        }
            //    }

            //    if ((condition == 2) && (energy < 128))
            //        condition = 3;
            //}

            //gfx_display_colour_text(16, 90, "Condition:", Color.Green);
            //gfx_display_text(190, 90, condition_txt[condition]);

            //str=string.Format("{0:d}.{1:d} Light Years", cmdr.fuel / 10, cmdr.fuel % 10);
            //gfx_display_colour_text(16, 106, "Fuel:", GFX_COL_GREEN_1);
            //gfx_display_text(70, 106, str);

            str=string.Format("{0:0.0} Cr", cmdr.credits);
            Helper.Fonts.WriteText(16, 130, "Cash:", Color.Green);
            Helper.Fonts.WriteText(350, 130, str);

            if (cmdr.legal_status == 0)
                str="Clean";
            else
                str=(cmdr.legal_status > 50)? "Fugitive" : "Offender";

             Helper.Fonts.WriteText(16, 166, "Legal Status:", Color.Green);
             Helper.Fonts.WriteText(350, 166, str);

            for (i = 0; i < NO_OF_RANKS; i++)
                if (cmdr.score >= rating[i].score)
                   str=rating[i].title;

            Helper.Fonts.WriteText(16, 202, "Rating:", Color.Green);
            Helper.Fonts.WriteText(350, 202, str);

            Helper.Fonts.WriteText(16, 238, "SHIP EQUIPMENT:", Color.Green);
            #region Equipment
            //x = EQUIP_START_X;
            //y = EQUIP_START_Y;

            Helper.Fonts.WriteText(16, 258, cmdr.ship.CurrentLaser.Name, Color.White);


            //if (cmdr.cargo_capacity > 20)
            //{
            //    gfx_display_text(x, y, "Large Cargo Bay");
            //    y += Y_INC;
            //}

            //if (cmdr.escape_pod)
            //{
            //    gfx_display_text(x, y, "Escape Pod");
            //    y += Y_INC;
            //}

            //if (cmdr.fuel_scoop)
            //{
            //    gfx_display_text(x, y, "Fuel Scoops");
            //    y += Y_INC;
            //}

            //if (cmdr.ecm)
            //{
            //    gfx_display_text(x, y, "E.C.M. System");
            //    y += Y_INC;
            //}

            //if (cmdr.energy_bomb)
            //{
            //    gfx_display_text(x, y, "Energy Bomb");
            //    y += Y_INC;
            //}

            //if (cmdr.energy_unit)
            //{
            //    gfx_display_text(x, y,
            //              cmdr.energy_unit == 1 ? "Extra Energy Unit" : "Naval Energy Unit");
            //    y += Y_INC;
            //    if (y > EQUIP_MAX_Y)
            //    {
            //        y = EQUIP_START_Y;
            //        x += EQUIP_WIDTH;
            //    }
            //}

            //if (cmdr.docking_computer)
            //{
            //    gfx_display_text(x, y, "Docking Computers");
            //    y += Y_INC;
            //    if (y > EQUIP_MAX_Y)
            //    {
            //        y = EQUIP_START_Y;
            //        x += EQUIP_WIDTH;
            //    }
            //}


            //if (cmdr.galactic_hyperdrive)
            //{
            //    gfx_display_text(x, y, "Galactic Hyperspace");
            //    y += Y_INC;
            //    if (y > EQUIP_MAX_Y)
            //    {
            //        y = EQUIP_START_Y;
            //        x += EQUIP_WIDTH;
            //    }
            //}

            //if (cmdr.front_laser)
            //{
            //    sprintf(str, "Front %s Laser", laser_type(cmdr.front_laser));
            //    gfx_display_text(x, y, str);
            //    y += Y_INC;
            //    if (y > EQUIP_MAX_Y)
            //    {
            //        y = EQUIP_START_Y;
            //        x += EQUIP_WIDTH;
            //    }
            //}

            //if (cmdr.rear_laser)
            //{
            //    sprintf(str, "Rear %s Laser", laser_type(cmdr.rear_laser));
            //    gfx_display_text(x, y, str);
            //    y += Y_INC;
            //    if (y > EQUIP_MAX_Y)
            //    {
            //        y = EQUIP_START_Y;
            //        x += EQUIP_WIDTH;
            //    }
            //}

            //if (cmdr.left_laser)
            //{
            //    sprintf(str, "Left %s Laser", laser_type(cmdr.left_laser));
            //    gfx_display_text(x, y, str);
            //    y += Y_INC;
            //    if (y > EQUIP_MAX_Y)
            //    {
            //        y = EQUIP_START_Y;
            //        x += EQUIP_WIDTH;
            //    }
            //}

            //if (cmdr.right_laser)
            //{
            //    sprintf(str, "Right %s Laser", laser_type(cmdr.right_laser));
            //    gfx_display_text(x, y, str);
            //}
#endregion
        }//display_commander_status()
    }
}
